<template>
  <a-form-model-item :label="label" class="form-item" :prop="name || ''">
    <a-input-number allowClear size="large" class="input-left" :min="0" :value="number.value" @change="handleNumberChange" />
  </a-form-model-item>
</template>
<script>
function validatePrimeNumber(number) {
  if (number === 11) {
    return {
      validateStatus: 'success',
      errorMsg: null
    }
  }
  return {
    validateStatus: 'error',
    errorMsg: 'The input number must be > 0!'
  }
}
export default {
  props: {
    label: {
      type: [String],
      default: '提示'
    },
    size: {
      type: [String],
      default: 'large'
    },
    form: {
      type: [Object],
      defaule: {},
      require: true
    },
    name: {
      type: [String],
      require: true
    },
    refreshKey: {
      type: [String],
      require: true
    },
    nType: {
      //数字校验类型
      type: String,
      default: 'number'
      // account number
    }
  },
  data() {
    return {
      labelCol: { span: 7 },
      wrapperCol: { span: 12 },
      number: {
        value: ''
      },
      tips: 'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.'
    }
  },
  watch: {
    refreshKey(n, o) {
      this.number.value = this.form[this.name]
    }
  },
  methods: {
    // 金额输入校验
    checkAmountInput(num) {
      if (Number(num) + '' == 'NaN') return false
      switch (true) {
        case num === '' || num == null:
          break
        case parseInt(num) != num:
          if (num.toString().split('.')[1].length > 2) {
            this.t.onceError('只能输入小数点后两位！')
            this.loseBlur()
            this.number.value = this.form[this.name]
            return false
          }
          break
        case num === 0:
          this.t.onceError('请输入大于0的金额！')
          return false
          break
        default:
          break
      }
      return true
    },
    checkNormalNumber(num) {
      // 普通数字输入校验
      return true
    },
    handleNumberChange(value) {
      let cr = true
      switch (true) {
        case this.nType === 'account':
          cr = this.checkAmountInput(value)
          break
        case this.nType === 'number':
          cr = this.checkNormalNumber(value)
          break
      }
      if (!cr) return
      this.number = {
        ...validatePrimeNumber(value),
        value
      }
      this.form[this.name] = value
    },
    loseBlur() {
      // 去焦点
      let is = document.getElementsByTagName('input')
      is.forEach(item => {
        item.blur()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.input-left {
  margin-left: 20px;
  width: 300px;
}
</style>
